/*
============================================================================
 Name		 : 	BluetoothManager.h
 Author	  	 : 
Copyright (c) Symbian Ltd 2008. All rights reserved.
 Description :  CRpsGameEngine is responsible for managing the Bluetooth piconet, updating the view,
				and controlling the logic of the game. CRpsGameEngine acts either as a Master or Slave.
			    The connection between a Master and a Slave is represented by the CBluetoothConnector
			    class while the Slave is represented by the CBluetoothResponder class.	
============================================================================
*/

#ifndef __RPSBLUETOOTHMANAGER_H__
#define __RPSBLUETOOTHMANAGER_H__

// INCLUDES
#include <bttypes.h> 
#include <es_sock.h>
#include <btdevice.h>
#include <bt_sock.h>
#include <btsdp.h>
#include <btmanclient.h>
#include <btextnotifiers.h>
#include "commoninterfaces.h"
#include "BluetoothResponder.h"

class CBluetoothDeviceDiscoverer;
class CBluetoothConnector;

class CBluetoothManager : public CBase, public MBluetoothDeviceDiscovererObserver,
										public MBluetoothObserver
	{
public:
	static CBluetoothManager* NewL(CGameScreenManager& aGameScreenMgr);
	~CBluetoothManager();
public:
	/**
	 * Called if the player has decided to control the game (Master role)
	 */
	void StartBtMaster();
	
	/**
	 * Called if the player has decided to wait for incoming connection (Slave role)
	 */
	void StartBtSlave();
	
	/**
	 * Send the player choice to the remote BT device
	 * @aparam aElement RPS's element
	 */
	void SendData(TRoshambo::TElement aElement);
	
	/**
	 * Quit the game
	 * @aparam aError KErrNone if the player decided to quit the game otherwise one of the system error
	 */
	void QuitMultiplayerGame();
	
	/**
	 * Called when the player that controls the game (Master role) decide to start the game
	 */
	void StartGame();
	
	/**
	 * Called when the player that controls the game (Master role) decide to start play again
	 */
	void ReplayGame();
	
	/**
	 * Query to see if the Connector/Responder are connected to the remote BT device
	 * @return ETrue if connected
	 */
	TBool Connected();
		
public:	//From MBluetoothDeviceDiscovererObserver,
	/**
	 * Callback from CBluetoothDeviceDiscoverer if an error occur during device discovering
	 * @param aError An error	 
	 */
	void OnDeviceDiscoveryErr(const TInt aError);
	
	/**
	 * RNotifier's callback
	 * aResponse Pckgbuf to retrieve the response from the device selection dialog via the Notifier framework
	 */
	void OnDeviceDiscoveryComplete(const TBTDeviceResponseParamsPckg& aResponse);
	
public:	//From MBluetoothObserver,
	/**
	 * Callback either from the Connector or Responder to report a connection error
	 * @param aConnHandle 	The handle of the Connector/Responder
	 * @param aError	An error		 
	 */
	void ConnectionErr(const TInt aConnHandle, const TInt aError);
	
	/**
	 * Callback either from the Connector or Responder to report incoming data from the remote BT device
	 * @param aConnHandle The handle of the Connector/Responder
	 * @param aData Description containing the data arrived from the remote BT device 
	 */
	void DataReceived(const TInt aConnHandle, const TDesC8& aData);
	
	/**
	 * Callback either from the Connector or Responder to report the sending data completion
	 * @param aConnHandle The handle of the Connector/Responder	
	 */
	void SendDataComplete(const TInt aConnHandle);

private:
	/**
	 * Utility function to display the RPS error to the player
	 */
	void DisplayError(const TInt aError);
	
	/**
	 * Creates and starts the bluetooth connection
	 */ 
	void StartConnectorL();
private: // Construction	
	
	CBluetoothManager(CGameScreenManager& aGameScreenMgr);
	
	void ConstructL();

private:
	/**
	* ETrue if the Connector/Responder are connected to the remote BT device 
	*/
	TBool iConnected;
	
	/**
	* Displays to the player the BT device in range to choose from
	*/
	CBluetoothDeviceDiscoverer* iBtDeviceDiscoverer;
	
	/**
	* The remote BT device address
	*/
	TBTDevAddr iBtDevAddr;
	
	/**
	* Class to allow parameters to be sent to the device selection dialog via the RNotifier API
	*/
	TBTDeviceSelectionParams iDevSelFilter;
	
	/**
	* A handle to an existing session on the socket server (ESock)
	*/
	RSocketServ iSocketServer;	
	
	/**
	* Connector/Responder base class pointer
	*/
	CBluetoothConnectionBase* iBtConnBase;
	
	/**
	* Game screen manager to call to update the screens
	*/
	CGameScreenManager& iGameScreenMgr;
	
	/**
	* Array of CBluetoothConnectionBase pointers. Not used in this example.
	* To be used in a multiplayer game with more then two players
	
	//RPointerArray<CBluetoothConnectionBase> iBtConnBaseArray;
	
	For example:
	TInt index = KErrNotFound;
	for(TInt i=0; i<iBtConnBaseArray.Count();i++)
	    {
	    if(iBtConnBaseArray[i]->Handle() == aConnHandle)
	        {
	        index = i;
	        }
	    }

	After executing the loop, if index is different to KErrNotFound, 
	we know that it was one of the connectors that called the callback 
	(if not, it was the Responder's handle). 
	
	When scaling the code for more than two players, it is important to note 
	the game should have a class that manages all the Connectors (i.e. CConnectionManager). 
	The CBluetoothManager class should own the CConnectionManager class.
	*/
	};

#endif // __RPSBLUETOOTHMANAGER_H__

// End of File
